
#CFLAGS=-static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles
CFLAGS=-static -mcmodel=medany -fvisibility=hidden -nostartfiles
GCC=/d/xpack/bin/riscv-none-elf-gcc
OBJDUMP = /d/xpack/bin/riscv-none-elf-objdump -d -M no-aliases -M numeric 
OBJCOPY_INST = /d/xpack/bin/riscv-none-elf-objcopy -j .text -j .text.startup -j .text.init -O binary 
OBJCOPY_DATA = /d/xpack/bin/riscv-none-elf-objcopy -j .data -O binary 

ELFS := elf/tetris.elf
DATAS := $(patsubst elf/%.elf,data/%.data,$(ELFS))
INSTS := $(patsubst elf/%.elf,inst/%.inst,$(ELFS))
IHEX := $(patsubst elf/%.elf,ihex/%_00.dat,$(ELFS))
DHEX := $(patsubst elf/%.elf,dhex/%_00.dat,$(ELFS))
BLSRC := tetris.c oled/oled.c oled/oled_main.c sparrow.c

all: $(DATAS) $(INSTS) $(IHEX) $(DHEX)
	./cp.sh tetris

data/%.data: elf/%.elf
	$(OBJCOPY_DATA) $< $@

inst/%.inst: elf/%.elf
	$(OBJCOPY_INST) $< $@

ihex/%_00.dat: inst/%.inst
	python3 inst.py $<

dhex/%_00.dat: data/%.data
	python3 data.py $<

elf/tetris.elf: $(BLSRC)
	$(GCC) -O2 -S -march=rv32i -mabi=ilp32 $(BLSRC)
	$(GCC) -O2 ${CFLAGS} -march=rv32i -mabi=ilp32 -Tlink.ld -o $@ $(BLSRC)
	$(OBJDUMP) $@ > dump/tetris.dump

clean:
	rm -f elf/* dump/* asm/* inst/* data/* ihex/* dhex/* cnt/* isim/* dsim/*

